package com.ym.b2b.product.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;

import com.alibaba.fastjson.JSON;
import com.ym.b2b.commons.dao.BaseDao;
import com.ym.b2b.product.dao.ProductDao;
import com.ym.b2b.product.sqlbean.EmBProduct;
import com.zc.commons.base.Page;

@Repository
public class ProductDaoImpl<T> extends BaseDao<T> implements ProductDao {

    @Override
    public Page<EmBProduct> searchPageProduct(EmBProduct emBProduct, Page<EmBProduct> page) throws DataAccessException {
        page.setTotalRow(searchPageProductNum(emBProduct));
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("map", emBProduct);
        map.put("startrow", page.getStartRow());
        map.put("pagesize", page.getPageSize());
        map.put("pageable", page.getPageable());
        page.setRows(list("EmBProduct.searchPageProduct", map));
        return page;
    }

    @Override
    public Long searchPageProductNum(EmBProduct emBProduct) throws DataAccessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("map", emBProduct);
        return (Long) listNum("EmBProduct.searchProductNum", map);
    }

    @Override
    public EmBProduct showProductDetail(EmBProduct emBProduct) throws DataAccessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("map", emBProduct);
        return (EmBProduct) find("EmBProduct.searchPageProduct", map);
    }

    @Override
    public EmBProduct showProductDetailConcise(EmBProduct emBProduct) throws DataAccessException {
        return (EmBProduct) find("EmBProduct.searchProduct", emBProduct);
    }

    @Override
    public void insertEmBProduct(EmBProduct emBProduct) throws DataAccessException {
        insert("EmBProduct.insertEmBProduct", emBProduct);

    }

    @Override
    public void deleteEmBProduct(EmBProduct emBProduct) throws DataAccessException {
        delete("EmBProduct.deleteEmBProduct", emBProduct);

    }

    @Override
    public List<EmBProduct> searchEmBProduct(EmBProduct emBProduct) throws DataAccessException {
        return list("EmBProduct.searchEmBProduct", emBProduct);
    }

    @Override
    public EmBProduct searchAllProductDetail(EmBProduct emBProduct) throws DataAccessException {
        return (EmBProduct) find("EmBProduct.searchAllProductDetail", emBProduct);
    }

    @Override
    public void updateEmBProduct(EmBProduct emBProduct) throws DataAccessException {
        update("EmBProduct.updateEmBProduct", emBProduct);

    }

    @Cacheable(value = "product", key = "#emBProduct.cacheKey", condition = "#emBProduct.searchFlag eq 'tag'")
    @Override
    public List<EmBProduct> searchSimpleProductList(EmBProduct emBProduct) throws DataAccessException {
        return list("EmBProduct.searchSimpleProductList", emBProduct);
    }

    @Override
    public Page<EmBProduct> searchManagerProductList(EmBProduct emBProduct, Page<EmBProduct> page)
                                                                                                  throws DataAccessException {
        page.setTotalRow(searchPageProductNum(emBProduct));
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("map", emBProduct);
        map.put("startrow", page.getStartRow());
        map.put("pagesize", page.getPageSize());
        map.put("pageable", page.getPageable());
        page.setRows(list("EmBProduct.searchPageProduct", map));
        return page;
    }

    @Override
    public Integer isInFavolite(String productCode) throws DataAccessException {
        return (Integer) find("EmBProduct.isInFavolite", productCode);
    }

	@Override
	public Integer hasProduct(String productCode) throws DataAccessException {
		 return (Integer) find("EmBProduct.hasProduct", productCode);
	}

}
